package com.yuexun.beilunpatient.ui.satisfaction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.main.bean.AreaBean;
import com.yuexun.beilunpatient.ui.main.bean.AreaHospBean;
import com.yuexun.beilunpatient.ui.main.model.impl.AreaHospModel;
import com.yuexun.beilunpatient.ui.main.presenter.impl.AreaHospPresenter;
import com.yuexun.beilunpatient.ui.main.ui.view.IAreaHospView;
import com.yuexun.beilunpatient.ui.satisfaction.bean.SatisfactionTypeBean;
import com.yuexun.beilunpatient.ui.satisfaction.model.impl.SatisfactionModel;
import com.yuexun.beilunpatient.ui.satisfaction.presenter.impl.SatisfactionTypeListPresenter;
import com.yuexun.beilunpatient.ui.satisfaction.ui.fragment.Frag_Satisfaction_DraftBox;
import com.yuexun.beilunpatient.ui.satisfaction.ui.fragment.Frag_Satisfaction_Get;
import com.yuexun.beilunpatient.ui.satisfaction.ui.fragment.Frag_Satisfaction_Submit;
import com.yuexun.beilunpatient.ui.satisfaction.ui.view.ISatisfactionTypeListView;
import com.yuexun.beilunpatient.widget.ActionSheetDialog;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/3/20 0020.
 */

public class Act_Satisfaction_Main extends BaseKJActivity implements ISatisfactionTypeListView, IAreaHospView {

    @Bind(R.id.get)
    TextView get;
    @Bind(R.id.get_img)
    ImageView getImg;
    @Bind(R.id.submit)
    TextView submit;
    @Bind(R.id.submit_img)
    ImageView submitImg;
    @Bind(R.id.draftbox)
    TextView draftbox;
    @Bind(R.id.draftbox_img)
    ImageView draftboxImg;
    @Bind(R.id.tv_area)
    TextView tvArea;
    @Bind(R.id.tv_unit)
    TextView tvUnit;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.et_search)
    EditText etSearch;

    private int pageType = 0;
    private KJFragment mContentFragment;
    private KJFragment fragment_get;
    private KJFragment fragment_submit;
    private KJFragment fragment_draftbox;
    private ICallBackListener mCallBack;
    SatisfactionTypeListPresenter typeListPresenter;
    AreaHospPresenter areaHospPresenter;
    private List<SatisfactionTypeBean> typeDatas = new ArrayList<>();
    private List<AreaBean> areaBeans = new ArrayList<>();
    private List<AreaHospBean> hospBeans = new ArrayList<>();
    private String typeId = "";
    private String areaId = "";
    private String hospId = "";

    public void setmCallBack(ICallBackListener mCallBack) {
        this.mCallBack = mCallBack;
    }

    private void refreshKey(){
        Bundle bundle = new Bundle();
        bundle.putString("typeId", typeId);
        bundle.putString("areaId", areaId);
        bundle.putString("hospId", hospId);
        bundle.putString("key", etSearch.getText().toString());
        mCallBack.callback(bundle);
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.act_satisfaction_main);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        typeListPresenter = new SatisfactionTypeListPresenter(new SatisfactionModel(), this);
        areaHospPresenter = new AreaHospPresenter(new AreaHospModel(), this);
        Map<String, String> params2 = new HashMap<>();
        areaHospPresenter.inquireAreaWithHospList(params2);
        ApiGetData();
        fragment_get = new Frag_Satisfaction_Get();
        fragment_submit = new Frag_Satisfaction_Submit();
        fragment_draftbox = new Frag_Satisfaction_DraftBox();
        mContentFragment = fragment_get;
        setmCallBack((ICallBackListener) mContentFragment);
        changeFragment(R.id.survey_content, mContentFragment);
    }

    //获取分类数据
    private void ApiGetData() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        typeListPresenter.getSatisfactionTypeList(params);
    }

    @OnClick({R.id.title_back, R.id.get_ll, R.id.submit_ll, R.id.draftbox_ll,
            R.id.tv_area, R.id.tv_unit, R.id.tv_type, R.id.tv_search,R.id.add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.get_ll:
                get.setTextColor(getResources().getColor(R.color.main_text_02));
                getImg.setBackgroundColor(getResources().getColor(R.color.main_text_02));
                submit.setTextColor(getResources().getColor(R.color.txt_black1));
                submitImg.setBackgroundColor(getResources().getColor(R.color.transparent));
                draftbox.setTextColor(getResources().getColor(R.color.txt_black1));
                draftboxImg.setBackgroundColor(getResources().getColor(R.color.transparent));
                mContentFragment = fragment_get;
                setmCallBack((ICallBackListener) mContentFragment);
                changeFragment(R.id.survey_content, mContentFragment);
                if (pageType != 0) {
                    etSearch.setText("");
                    pageType = 0;
                }
                break;
            case R.id.submit_ll:
                get.setTextColor(getResources().getColor(R.color.txt_black1));
                getImg.setBackgroundColor(getResources().getColor(R.color.transparent));
                submit.setTextColor(getResources().getColor(R.color.main_text_02));
                submitImg.setBackgroundColor(getResources().getColor(R.color.main_text_02));
                draftbox.setTextColor(getResources().getColor(R.color.txt_black1));
                draftboxImg.setBackgroundColor(getResources().getColor(R.color.transparent));
                mContentFragment = fragment_submit;
                setmCallBack((ICallBackListener) mContentFragment);
                changeFragment(R.id.survey_content, mContentFragment);
                if (pageType != 1) {
                    etSearch.setText("");
                    pageType = 1;
                }
                break;
            case R.id.draftbox_ll:
                get.setTextColor(getResources().getColor(R.color.txt_black1));
                getImg.setBackgroundColor(getResources().getColor(R.color.transparent));
                submit.setTextColor(getResources().getColor(R.color.txt_black1));
                submitImg.setBackgroundColor(getResources().getColor(R.color.transparent));
                draftbox.setTextColor(getResources().getColor(R.color.main_text_02));
                draftboxImg.setBackgroundColor(getResources().getColor(R.color.main_text_02));
                mContentFragment = fragment_draftbox;
                setmCallBack((ICallBackListener) mContentFragment);
                changeFragment(R.id.survey_content, mContentFragment);
                if (pageType != 2) {
                    etSearch.setText("");
                    pageType = 2;
                }
                break;
            case R.id.tv_area:
                selectAreaDialog();
                break;
            case R.id.tv_unit:
                if(!areaId.isEmpty()){
                    selectHospDialog();
                }else{
                    ViewInject.toast("没获取到区域");
                }
                break;
            case R.id.tv_type:
                selectTypeDialog();
                break;
            case R.id.tv_search:
                refreshKey();
                break;
            case R.id.add:
             startActivity(new Intent(aty,Act_Satisfaction_New.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void getSatisfactionTypeList(BaseEntity<SatisfactionTypeBean> bean) {
        if ("200".equals(bean.getStatus())) {
            typeDatas = bean.getDatas();
        } else {
            ViewInject.toast(bean.getMessage());
        }
    }

    @Override
    public void getAreaWithHospList(BaseEntity<AreaBean> baseEntity) {
        if(baseEntity.getStatus().equals("200")){
            areaBeans = baseEntity.getDatas();
            areaId = areaBeans.get(0).getId() + "";
            hospBeans = areaBeans.get(0).getHospList();
            refreshKey();
        }
    }

    /**
     * 选择类型
     */
    private void selectTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);

        actionSheetDialog.addSheetItem("全部", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        tvType.setText("全部");
                        typeId = "";
                        refreshKey();
                    }
                });
        for (int i = 0; i < typeDatas.size(); i++) {
            final int final1 = i;
            actionSheetDialog.addSheetItem(typeDatas.get(i).getTitle(), ActionSheetDialog.SheetItemColor.Blue,
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {
                            tvType.setText(typeDatas.get(final1).getTitle());
                            typeId = typeDatas.get(final1).getTypeId() + "";
                            refreshKey();
                        }
                    });
        }
        actionSheetDialog.show();
    }

    /**
     * 选择区域
     */
    private void selectAreaDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);

        for (int i = 0; i < areaBeans.size(); i++) {
            final int final1 = i;
            actionSheetDialog.addSheetItem(areaBeans.get(i).getAreaName(), ActionSheetDialog.SheetItemColor.Blue,
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {
                            tvArea.setText(areaBeans.get(final1).getAreaName());
                            areaId = areaBeans.get(final1).getId() + "";
                            hospBeans = areaBeans.get(final1).getHospList();
                            refreshKey();
                        }
                    });
        }
        actionSheetDialog.show();
    }

    /**
     * 选择医院
     */
    private void selectHospDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);

        for (int i = 0; i < hospBeans.size(); i++) {
            final int final1 = i;
            actionSheetDialog.addSheetItem(hospBeans.get(i).getHospitalName(), ActionSheetDialog.SheetItemColor.Blue,
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {
                            tvUnit.setText(hospBeans.get(final1).getHospitalName());
                            hospId = hospBeans.get(final1).getId() + "";
                            refreshKey();
                        }
                    });
        }
        actionSheetDialog.show();
    }
}
